import uuid, base64, json, pickle, platform
from hashlib import md5

def bytesSerializToStr(data):
    # 将bytes序列化成str  <class 'bytes'> --> <class 'str'>
    ret = pickle.dumps(data).decode('latin1')
    return ret

def strSerializTobytes(data):
    # 将str反序列化成bytes <class 'str'> --> <class 'bytes'>
    ret = pickle.loads(data.encode('latin1'))
    return ret

def get_mac_address():
    # mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
    # # maac = ":".join([mac[e:e + 2] for e in range(0, 11, 2)])
    # maaac = "-syn-".join([mac[e:e + 2] for e in range(0, 11, 2)])
    # return maaac.upper()
    plat = platform.system().lower()
    if 'windows' == plat:
        mac_address = './address'
    else:
        mac_address = '/opt/class_net_eth0/address'
    try:
        with open(mac_address) as f:
            mac = f.read()
        mac = mac.replace(':', '-syn-')
    except FileNotFoundError:
        msg = f'{mac_address}不存在'
        print('synlog=', msg)
    except PermissionError:
        msg = f'{mac_address}存在但没有权限访问'
        print('synlog=', msg)
    except Exception as e:
        msg = f'{mac_address}异常{str(e)}'
        print('synlog=', msg)

    return mac.upper()

def get_pre_license():
    # 获取授权码
    mac_mixStr = get_mac_address()
    mac_mixStrMd5 = md5(mac_mixStr.encode('utf-8')).hexdigest().upper()
    pre_license = list(mac_mixStrMd5)
    pre_license.insert(4, 's')
    pre_license.insert(8, 'y')
    pre_license.insert(12, 'n')
    pre_license.insert(16, 'j')
    pre_license.insert(20, 'o')
    pre_license.insert(24, 'n')
    pre_license.insert(28, 'e')
    pre_license.insert(32, 's')
    pre_license = ''.join(pre_license)
    return pre_license

def get_license(src_fileName, dest_fileName):
    # 根据授权文件信息生成加密授权文件
    src_contents = ''
    with open(src_fileName, encoding='utf-8') as f:
        src_contents = f.read()

    src_contents = json.loads(src_contents)
    if len(src_contents) < 2:
        print(f'未检测到原始加密文件, path={src_fileName}')
        return False

    # dict --> bytes
    src_contents = bytes('{}'.format(src_contents), 'utf-8')
    # bytes --> base64
    dest_contents = base64.encodebytes(src_contents)
    # # 对 base64 进行混合加密
    # dest_contents_base64_mix = bytes('s', 'utf-8') + dest_contents + bytes('yn', 'utf-8')
    # print('base64_mix: ', dest_contents_base64_mix)

    # bytes 序列化 str
    dest_str = bytesSerializToStr(dest_contents)
    with open(dest_fileName, 'w', encoding='utf-8') as f:
        f.write(dest_str)

    return True

def get_licensees_info(src_fileName):
    # 解析加密授权文件信息
    licensees_contents = ''
    with open(src_fileName, encoding='utf-8') as f:
        licensees_contents = f.read()

    if len(licensees_contents) < 1:
        return False
        
    # str 反序列化 bytes
    licensees_contents = strSerializTobytes(licensees_contents)
    # base64 --> bytes
    src_contents = base64.decodebytes(licensees_contents)
    # bytes --> dict
    license_info = eval(str(src_contents, 'utf-8'))
    return license_info

    
